<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>内容管理</title>
<script type="text/javascript" src="/res/third/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="/res/third/ueditor/ueditor.all.js"></script>
<script type="text/javascript" src="/res/third/ueditor/lang/zh-cn/zh-cn.js"></script>
<style>
.file_txt{position: absolute;
top: -11px;
left: 141px;
height: 82px;
width: 400px;
}
.delname{
font-size:16px;
color:red;
text-decoration:none;
float:right;
padding-right:3px;
position: relative;
top:0px;
}


</style>
<script type="text/javascript">
	var editor;
	$(function(){
		
		var option = {
				theme : "default",
				lang : "zh-cn",
				initialFrameWidth : 700,
				initialFrameHeight : 350,
				autoHeightEnabled : false,
				pasteplain : false,//默认纯文本格式粘贴,
				elementPathEnabled : false,
				focus:false,//初始化时不让编辑器获取焦点
				toolbars : [ [ 'fullscreen', 'source', 'undo', 'redo', 'bold','indent',
								'italic', 'underline', 'strikethrough', 'justifyleft',
								'justifyright', 'justifycenter', 'justifyjustify',
								'rowspacingtop', 'rowspacingbottom', 'lineheight',
								'removeformat', '|', 'fontfamily', 'fontsize', 'forecolor',
								'backcolor', 'insertorderedlist', 'insertunorderedlist',
								'insertimage', 'emotion','inserttable','mergeright','mergedown','deleterow','deletecol'
								,'splittorows','splittocols','splittocells','deletecaption','mergecells','deletetable', 'link'] ]
			};
		editor = UE.getEditor('container' , option);

		var treeData;

		$.ajax({
			url : '/content/v_tree.do',
			type : 'post',
			dataType : 'json',
			async : false,
			success : function(data){
				$("#channelId").combotree({
					data : data,
					panelWidth : 180
				});
			}
		});

		$("span[class='combo']").css({"height" : "30px" , "border-radius": "4px" , "width":"180px"});
		$("span[class='combo-arrow']").css({"height" : "30px"});
		$("input[class^='combo-text']").css({"height" : "30px" , "width":"160px"});
		
	})
	
	
	var exceldialog=null;
	function uploaddialog(){
		exceldialog = art.dialog.uploadFile("上传图片",{callback:"uploadcallback",userpath:"admin",extensions:"gif,jpg,png,jepg"});
	}
	
	function uploadcallback(response){
		exceldialog.close();
		$("#showTitleImg").text(response.url);
		$("#titleImg").val(response.url);
		$("#showTitleImg").attr("href" , response.url);
		
	}


	function validateForm(){
		var msg = "";

		var title = $("#title").val().trim();


		if(title == ""){
			msg += "请输入标题<br/>";
		}else{
			if(title.length>200){
			 msg += "标题长度不能超过200<br/>";
			}
		}
      
       var editorname =$("#editor").val().trim();
       if(editorname.length>20){
			 msg += "作者不能超过20<br/>";
        }
       var hit =$("#hit").val().trim();
       if(hit == ""){
			 msg += "浏览量不能为空，可以是0<br/>";
        }
       var seq =$("#seq").val().trim();
       if(seq == ""){
			 msg += "请填写排序号<br/>";
        }
		var content = editor.getContent().trim();//验证是否填写内容
	  
		if(content == ""){
			msg += "请填写内容<br/>";
		}

		/* if($("#ifImg").val() == 1 && $(".uploadify-queue-item").length == 0 && $("input[name='titleImg']").val() == ""){
			msg += "请选择图片<br/>";
		} */

		if(msg != ""){
			artDialog.alert(msg);
			return false;
		}

		//$("#contentForm").submit();
		/* if($(".uploadify-queue-item").length>0){
			$('#titleImg').uploadify('upload', '*');
		}else{ */
			$.ajax({
	 			   type: "POST",
	 			   dataType:"json",
	 			   url: "/content/o_add.do",
	 			   data:$("#contentForm").serialize(),//将form表单的内容序列化	
	 			   beforeSend:function(){
	 				   art.dialog.tips("加载中...");
	 			   },
	 			   success: function(data){
	 				   if(data.success){
	 					 //location.href="/content/v_content.do?queryTitle=" + queryTitle + "&queryCid=" + queryCid;
	 					 backToContent();
	 				   }else{
	 					  artDialog.alert(data.msg , function(){backToContent();});
	 				   }
	 			   }
	 		});
		//}
	}
  var queryTitle = window.encodeURIComponent(window.encodeURIComponent('${queryTitle!}'));
  var queryCid = '${queryCid!}';
	function backToContent(){
		$.messager.progress({
			text : "loadding",
			interval : 100
		});
		$("#center").load('/content/v_list.do?so_channelId=${channelId}' , function(){
			$.messager.progress('close');
		});
	}
	function resetAll(){//重置 
		$("#title").val("");
		$("#editor").val("");
		$("#seq").val("");
		editor.execCommand('cleardoc');
		$("#showTitleImg").text("");
		$("#titleImg").val("");
		$("#showTitleImg").attr("href" , "");
	}
</script>
</head>
<body>
<!-- 新建 -->
	   <div class="panel panel-info">
	        <div class="panel-heading">
	          <h3 class="panel-title">[#if content??]编辑[#else]新增[/#if]新闻</h3>
	        </div>
        <div class="panel-body">
			<form class="form-horizontal" role="form" action="/content/saveAdd.do"  id="contentForm">
				<input type="hidden" name="id" value="[#if content??]${content.id}[/#if]" />
				<input type="hidden" name="channel.id" value="${channelId!}" />
				<input type="hidden" name="channelId" value="${channelId!}" />
			  <div class="form-group" style="margin-top:15px;">
			    <label for="title" class="col-sm-2 control-label"><span style="color:red;">*</span>内容标题</label>
			    <div class="col-sm-5">
			      <input type="text" class="form-control" id="title" placeholder="请输入标题" name="title" required value="[#if content??]${content.title!}[/#if]">
			    </div>
			  </div>
			  <div class="form-group" >
			    <label for="title" class="col-sm-2 control-label">副标题</label>
			    <div class="col-sm-5">
			      <textarea  class="form-control" id="subTitle" placeholder="请输入副标题" name="subTitle" >[#if content??]${content.subTitle!}[/#if]</textarea>
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="title" class="col-sm-2 control-label">作者</label>
			    <div class="col-sm-2">
			      <input type="text" class="form-control" id="editor" placeholder="作者" name="editor" required value="[#if content??]${content.editor!}[/#if]">
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="title" class="col-sm-2 control-label">浏览量</label>
			    <div class="col-sm-2">
			      <input type="text" class="form-control" id="hit" placeholder="浏览量" name="hit" required value="[#if content??]${content.hit!}[#else]0[/#if]">
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="title" class="col-sm-2 control-label">是否推荐</label>
			    <div class="col-sm-2">
			        <select class="form-control" name="ifRecommend" id="ifRecommend">
					  <option value="0" [#if content?? && content.ifRecommend! == 0]selected="selected"[/#if]>否</option>
					  <option value="1" [#if content?? && content.ifRecommend! == 1]selected="selected"[/#if]>是</option>
					</select>
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="title" class="col-sm-2 control-label"><span style="color:red;">*</span>排序号</label>
			    <div class="col-sm-1">
			      <input type="text" class="form-control" id="seq" placeholder="排序号" name="seq" required value="[#if content??]${content.seq!}[/#if]">
			    </div>
			  </div>
			  <div class="form-group"  id="imgDiv">
			    <label for="ifImg" class="col-sm-2 control-label">标题图</label>
			    <div class="col-sm-1" style="position: relative;">
			    	<button type="button" class="btn btn-info" onclick="uploaddialog();" >上传图片</button>
			    	<input type="hidden" name="titleImg" value="[#if content??]${content.titleImg!}[/#if]" id="titleImg"/>
			    	<span style="font-size: 17px;"><a id="showTitleImg" target="_blank" href="[#if content??]${content.titleImg!}[/#if]">[#if content??]${content.titleImg!}[/#if]</a></span>
			    </div>
			  </div>
			  
			  <div class="form-group">
			    <label for="content" class="col-sm-2 control-label"><span style="color:red;">*</span>内容</label>
			    <div class="col-sm-10">
			    	<script id="container" name="content" type="text/plain">[#if content??]${content.content!}[/#if]</script>
			    </div>
			  </div>
			  <div class="form-group">
			    <div class="col-sm-offset-2 col-sm-8">
			      <button type="button" class="btn btn-info" onclick="validateForm();">提交</button>
			      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			      <button type="button" class="btn btn-success" onclick="resetAll();">重置</button>
			      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			      <button type="button" class="btn btn-success" onclick="backToContent();">
			  		<span class="glyphicon glyphicon-chevron-left"></span>返回
			  	</button>
			    </div>
				  <div class="col-sm-2">
				  [#if token?? && token != '']
                      <button type="button" class="btn btn-info" onclick="preview('${token!}')">预览</button>
				  [/#if]
				  </div>
			  </div>
			</form>
        </div>
      </div>

<script>
	function preview(token) {
		var param = {};
		param.token = token;
		param.title = $('#title').val();
        param.subTitle = $('#subTitle').val();
        param.editor = $('#editor').val();
        param.hit = $('#hit').val();
		param.content = editor.getContent();
		param.pubdate = '[#if (content.createtime)??]${content.createtime?date!}[#else]${now!}[/#if]';
		param.channelId = '${channelId!}';
        subForm(param);
	}

	function subForm(params) {
        var temp = document.createElement("form");
        temp.action = 'http://${WEB_SITE!}/content/preview';
        temp.method = "post";
        temp.style.display = "none";
        temp.target = '_blank';
        for (var x in params) {
            var opt = document.createElement("textarea");
            opt.name = x;
            opt.value = params[x];
            temp.appendChild(opt);
        }
        document.body.appendChild(temp);
        temp.submit();
		return temp;
	}
</script>
</body>
</html>